package com.nd.qa.shareplate.script.robotium;

import com.nd.qa.shareplate.common.SysConstant;
import com.nd.qa.shareplate.model.PageElement;
import com.nd.qa.shareplate.util.CharacterUtils;

/**
 * robotiumAPI管理类
 * 
 * @version 1.00 wuqiaomin 新建
 *
 */
public class Robotium {

	public String scriptInit(String modelName,String projectUserName,String projectUserPwd, String scriptName,
			String packagename, String activityName) {
		String initStr = "package %s.test;\n"
				+ "import com.nd.qa.test.common.NewSolo;\n"
				+ "import com.nd.qa.test.common.InputDataStore;\n"
				+ "import android.test.ActivityInstrumentationTestCase2;\n"
				+ "import com.nd.qa.test.common.InputDataStore;"
				+ "import android.app.Activity;\n"
				+ "import android.view.KeyEvent;\n"
				+ "import android.util.Log;\n"
				+ "import android.app.Instrumentation;\n"
				+ "import android.app.Instrumentation.ActivityMonitor;\n"
				+ "import android.content.Intent;\n"
				+ "import android.widget.*;\n"
				+ "@SuppressWarnings(\"rawtypes\")\n"
				+ "//%s\n"
				+ "public class %s extends ActivityInstrumentationTestCase2 {\n"
				+ "    private NewSolo solo;\n"
				+ "    private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME = \"%s\";\n"
				+ "    private static Class<?> launcherActivityClass;\n"
				+ "    Activity mActivity;\n"
				+ "    static{\n"
				+ "        try {\n"
				+ "            launcherActivityClass = Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);\n"
				+ "        } catch (ClassNotFoundException e) {\n"
				+ "            throw new RuntimeException(e);\n"
				+ "        }\n"
				+ "    }\n"
				+ "    @SuppressWarnings(\"unchecked\")\n"
				+ "    public %s() throws ClassNotFoundException {\n"
				+ "        super(launcherActivityClass);\n"
				+ "    }\n"
				+ "    public void setUp() throws Exception {\n"
				+ "        //DataCleanManager.cleanApplicationData(getInstrumentation().getTargetContext());\n"
				+ "	       solo = new NewSolo(getInstrumentation(),getActivity());\n"
				+ "        solo.setup();\n"
				+ "        super.setUp();\n" + "  }\n" 
				+ " 	@Override\n"
				+ "    public void tearDown() throws Exception {\n"
				+ "        solo.newTeardown();\n" + "    }\n"
				+ "    public void test_Alogin() throws Exception{\n"
				+ "     solo.scrollToSide(solo.RIGHT);\n"
				+ "		solo.sleep(2000);\n"
				+ "		solo.scrollToSide(solo.RIGHT);\n"
				+ "		solo.sleep(2000);\n"
				+ "		solo.scrollToSide(solo.RIGHT);\n"
				+ "		solo.sleep(2000);\n"
				+ "		solo.scrollToSide(solo.RIGHT);\n"
				+ "		solo.sleep(2000);\n"
				+ "		solo.scrollToSide(solo.RIGHT);\n"
				+ "		solo.sleep(2000);\n"
				+ "		solo.scrollToSide(solo.RIGHT);\n"
				+ "		solo.sleep(2000);\n"
				+ "		solo.scrollToSide(solo.RIGHT);\n"
				+ "		solo.sleep(2000);\n"
				+ "		solo.scrollToSide(solo.RIGHT);\n"
				+ "		solo.sleep(2000);\n"
				+ "		solo.clickOnView(\"iv_guide_go_on\", 0, \"ND\", \"引导页\", \" \", 0, \" \");\n"
				+ "		solo.sleep(2000);\n"
				+ "		solo.enterText(0, \"%s\", \"登录页面\", \"用户登录\", \"%s\", 0, \"\");\n"
				+ "		solo.sleep(2000);\n"
				+ "		solo.enterText(1, \"%s\", \"登录页面\", \"用户登录\", \"%s\", 0, \"\");\n"
				+ "		solo.sleep(2000);\n"
				+ "		solo.clickOnText(\"登录\", \"登录页面\", \"点击登录按钮\", \"\", 0, \"\");\n"
				+ "		solo.sleep(2000);\n"
				+ "     }\n"
				+ " 	@Override\n"
				+"		public Activity getActivity() { \n"
				+"		if (mActivity == null) {\n"
				+"		Intent intent = new Intent(getInstrumentation().getTargetContext(), launcherActivityClass);\n"	
				+"		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);\n"
				+" 		ActivityMonitor monitor = getInstrumentation().addMonitor(launcherActivityClass.getName(), null, false);\n"
				+"		getInstrumentation().getTargetContext().startActivity(intent);\n"
				+"		mActivity = (Activity) getInstrumentation().waitForMonitor(monitor);\n"
				+"		setActivity(mActivity);\n"
				+"		}\n"
				+"		return mActivity;\n "
				+"		}\n"
				+"      @Override\n"
				+"      protected void runTest() throws Throwable {\n"
				+"         int retryTimes = InputDataStore.getInstance().getInput_Retrytime();\n"	
				+"			while(retryTimes > 0){\n"
				+"				try{\n"
				+"					Log.e(\"ROBOT\", \"异常，重新运行第\" + retryTimes + \"次\");\n"
				+"					super.runTest();\n"
				+"					break;\n"
				+"				} catch (Throwable e){\n"
				+"					if(retryTimes > 1) {\n"
				+"						Log.e(\"ROBOT\", \"fail，重跑--\"+retryTimes);\n"
				+"						retryTimes--;\n"
				+"						 continue;\n"
				+"				}else{\n"
				+"					throw e;\n"
				+"				}\n"
				+"			}\n"
				+"		}\n"
				+"	}\n";
				
		return String.format(initStr, packagename, modelName, scriptName,
				activityName, scriptName,projectUserName,projectUserName,projectUserPwd,projectUserPwd);
	}

	public String sleep(int time) {
		return String.format("solo.sleep(%d);\n", time);
	}

	public String click(PageElement element) {
		String code = "";
		System.out.println("element================="+element.getElementActionId()+","+element.getElementActionName());
		if (element.getElementTypeFlag() == 0) {
			if (element.getElementActionId()==1) {
				code =  clickOnView(element);
			}else if (element.getElementActionId()==7) {
				code = clickLongOnView(element);
			}
		} else {
			code = clickOnWebElement(element);
		}
		return code;
	}
	/**
	 * 查询元素信息
	 * @param element
	 * @return
	 */
	public String query(PageElement element){
		String code = "";
		String elementOperateDesc = element.getElementOperateDesc();
		String elementExpectName = element.getElementExpectName();
		Integer elementExpectOrder = element.getElementExpectOrder();
		String elementExpectResult =element.getElementExpectResult();
		if (element.getElementTypeName().equals(SysConstant.ELEMENT_TYPE_ID)) {
				code = "solo.queryByText(\"%s\",%s, \"%s\",\"%s\",\"%s\",%s,\"%s\");\n";
			return String.format(code, element.getElementContent(),element.getElementOrder(),element.getPageName(),
					elementOperateDesc==null?"":elementOperateDesc,elementExpectName==null?"":elementExpectName,elementExpectOrder==null?0:elementExpectOrder,elementExpectResult==null?"":elementExpectResult);
		}else {
			if (element.getElementExpectName()==null||element.getElementExpectName().trim().length()==0) {
				code = "solo.queryByText(%s.class, %s,\"%s\",\"%s\",\"%s\",%s,\"%s\");\n";
			}else if ("Text".equals(element.getElementExpectName())) {
				code = "solo.queryByText(%s.class, %s,\"%s\",\"%s\",\"%s\",%s,\"%s\");\n";
			}else{
				code = "solo.queryByText(%s.class, %s,\"%s\",\"%s\",%s.class,%s,\"%s\");\n";
			}
			return String.format(code, element.getElementTypeName(),element.getElementOrder(),element.getPageName(),
					elementOperateDesc==null?"":elementOperateDesc,elementExpectName==null?"":elementExpectName,elementExpectOrder==null?0:elementExpectOrder,elementExpectResult==null?"":elementExpectResult);
		}
	}
	/**
	 * 
	 * @param element
	 * @return
	 */
	public String queryByKeyBoard(PageElement element){
		String code = "";
		String elementOperateDesc = element.getElementOperateDesc();
		String elementExpectName = element.getElementExpectName();
		Integer elementExpectOrder = element.getElementExpectOrder();
		String elementExpectResult =element.getElementExpectResult();
		//code = "solo.sendKey(KeyEvent.KEYCODE_ENTER);\n";
		if (element.getElementExpectName()==null||element.getElementExpectName().trim().length()==0) {
			code = "solo.pressSoftKeyboardSearch(\"%s\",\"%s\",\"%s\",%s,\"%s\");\n";
		}else if ("Text".equals(element.getElementExpectName())) {
			code = "solo.pressSoftKeyboardSearch(\"%s\",\"%s\",\"%s\",%s,\"%s\");\n";
		}else{
			code = "solo.pressSoftKeyboardSearch(\"%s\",\"%s\",%s.class,%s,\"%s\");\n";
		}
		/*code = code+"solo.enterText(0, \"ItemToSearchFor\");\n";
		code = code+"solo.sleep(1000);\n";
		code = code+"solo.clickInList(0);\n";*/
		return String.format(code, element.getPageName(),elementOperateDesc==null?"":elementOperateDesc,elementExpectName==null?"":elementExpectName,elementExpectOrder==null?0:elementExpectOrder,elementExpectResult==null?"":elementExpectResult);
		/*return String.format(code, element.getElementContent(),element.getElementOrder(),element.getPageName(),
				elementOperateDesc==null?"":elementOperateDesc,elementExpectName==null?"":elementExpectName,elementExpectOrder==null?0:elementExpectOrder,elementExpectResult==null?"":elementExpectResult);
		*/
	}
	public String  dragForView(PageElement element){
		String code = "";
		String content = element.getElementContent();
		String text = element.getElementText();
		String tx[] = text.split(",");
		code = "solo.dragPage(\"%s\",(float)%s,(float)%s,(float)%s,(float)%s);\n";
		return String.format(code,content,tx[0],tx[1],tx[2],tx[3]);
	}
	public String enter(PageElement element) {
		if (element.getElementTypeFlag() == 0) {
			return enterText(element);
		} else {
			return enterTextInWebElement(element);
		}
	}

	public String drag(PageElement element) {
		if (element.getElementTypeFlag() == 0) {
			return scroll(element);
		} else {
			return dragOnView(element);
		}
	}
	/**
	 * 长按类型
	 * @param element
	 * @return
	 */
	private String clickLongOnView(PageElement element) {
		String code = "";
		String elementOperateDesc = element.getElementOperateDesc();
		String elementExpectName = element.getElementExpectName();
		Integer elementExpectOrder = element.getElementExpectOrder();
		String elementExpectResult =element.getElementExpectResult();
		if (element.getElementTypeName().equals(SysConstant.ELEMENT_TYPE_TEXT)) {
				code = "solo.clickLongOnText(\"%s\", %s ,\"%s\",\"%s\",\"%s\",%s,\"%s\");\n";
			return String.format(code,
					element.getElementContent(), element.getElementOrder(),element.getPageName(),
					elementOperateDesc==null?"":elementOperateDesc,elementExpectName==null?"":elementExpectName,elementExpectOrder==null?0:elementExpectOrder,elementExpectResult==null?"":elementExpectResult);
		} else if (element.getElementTypeName().equals(SysConstant.ELEMENT_TYPE_ID)) {
			//code = "solo.clickOnView(solo.getView(\"%s\",%s));\n";
				code = "solo.clickLongOnView(\"%s\",%s, \"%s\",\"%s\",\"%s\",%s,\"%s\");\n";
			return String.format(code, element.getElementContent(),element.getElementOrder(),element.getPageName(),
					elementOperateDesc==null?"":elementOperateDesc,elementExpectName==null?"":elementExpectName,elementExpectOrder==null?0:elementExpectOrder,elementExpectResult==null?"":elementExpectResult);
		} else if(element.getElementTypeName().equals(SysConstant.ELEMENT_TYPE_IMAGEVIEW)){
			if (element.getElementExpectName()==null||element.getElementExpectName().trim().length()==0) {
				code = "solo.clickLongOnView(%s.class, %s, \"%s\",\"%s\",\"%s\",%s,\"%s\");\n";
			}else {
				code = "solo.clickLongOnView(%s.class, %s, \"%s\",\"%s\",%s.class,%s,\"%s\");\n";
			}
			return String.format(code, element.getElementTypeName(),element.getElementOrder(),element.getPageName(),
					elementOperateDesc==null?"":elementOperateDesc,elementExpectName==null?"":elementExpectName,elementExpectOrder==null?0:elementExpectOrder,elementExpectResult==null?"":elementExpectResult);
		}else if (element.getElementTypeName().equals(SysConstant.ELEMENT_TYPE_XPATH)) {
			code = "solo.clickLongOnScreen(%s, %s, 4000,\"%s\",\"%s\",\"%s\",%s,\"%s\");\n";
			//解析element.getElementContent()
			String[] xyStr = element.getElementContent().split(",");
			return String.format(code, xyStr[0],xyStr[1],element.getPageName(),
					elementOperateDesc==null?"":elementOperateDesc,elementExpectName==null?"":elementExpectName,elementExpectOrder==null?0:elementExpectOrder,elementExpectResult==null?"":elementExpectResult);
		}else {
			if (element.getElementExpectName()==null||element.getElementExpectName().trim().length()==0) {
				code = "solo.clickLongOnView(%s.class, %s, \"%s\",\"%s\",\"%s\",%s,\"%s\");\n";
			}else {
				code = "solo.clickLongOnView(%s.class, %s, \"%s\",\"%s\",%s.class,%s,\"%s\");\n";
			}
			return String.format(code, element.getElementTypeName(),element.getElementOrder(),element.getPageName(),
					elementOperateDesc==null?"":elementOperateDesc,elementExpectName==null?"":elementExpectName,elementExpectOrder==null?0:elementExpectOrder,elementExpectResult==null?"":elementExpectResult);
		}
	}
	//,\"%s\",%s.class,%s,\"%s\"
	private String clickOnView(PageElement element) {
		String code = "";
		String elementOperateDesc = element.getElementOperateDesc();
		String elementExpectName = element.getElementExpectName();
		Integer elementExpectOrder = element.getElementExpectOrder();
		String elementExpectResult =element.getElementExpectResult();
		if (element.getElementTypeName().equals(SysConstant.ELEMENT_TYPE_TEXT)) {
				code = "solo.clickOnText(\"%s\", %s ,\"%s\",\"%s\",\"%s\",%s,\"%s\");\n";
			return String.format(code,
					element.getElementContent(),element.getElementOrder(),element.getPageName(),
					elementOperateDesc==null?"":elementOperateDesc,elementExpectName==null?"":elementExpectName,elementExpectOrder==null?0:elementExpectOrder,elementExpectResult==null?"":elementExpectResult);
		} else if (element.getElementTypeName().equals(SysConstant.ELEMENT_TYPE_ID)) {
			//code = "solo.clickOnView(solo.getView(\"%s\",%s));\n";
				code = "solo.clickOnView(\"%s\",%s, \"%s\",\"%s\",\"%s\",%s,\"%s\");\n";
			return String.format(code, element.getElementContent(),element.getElementOrder(),element.getPageName(),
					elementOperateDesc==null?"":elementOperateDesc,elementExpectName==null?"":elementExpectName,elementExpectOrder==null?0:elementExpectOrder,elementExpectResult==null?"":elementExpectResult);
		} else if (element.getElementTypeName().equals(SysConstant.ELEMENT_TYPE_IMAGEBUTTON)) {
				code = "solo.clickOnImageButton(%s, \"%s\",\"%s\",\"%s\",%s,\"%s\");\n";
			return String.format(code,element.getElementOrder(),element.getPageName(),
					elementOperateDesc==null?"":elementOperateDesc,elementExpectName==null?"":elementExpectName,elementExpectOrder==null?0:elementExpectOrder,elementExpectResult==null?"":elementExpectResult);
		} else if (element.getElementTypeName().equals(SysConstant.ELEMENT_TYPE_XPATH)) {//TODO
				code = "solo.clickOnScreen(%s, %s,\"%s\",\"%s\",\"%s\",%s,\"%s\");\n";
			//解析element.getElementContent()
			String[] xyStr = element.getElementContent().split(",");
			return String.format(code, xyStr[0],xyStr[1],element.getPageName(),
					elementOperateDesc==null?"":elementOperateDesc,elementExpectName==null?"":elementExpectName,elementExpectOrder==null?0:elementExpectOrder,elementExpectResult==null?"":elementExpectResult);
		} else if(element.getElementTypeName().equals(SysConstant.ELEMENT_TYPE_IMAGEVIEW)){
			if (element.getElementExpectName()==null||element.getElementExpectName().trim().length()==0) {
				code = "solo.clickOnImageView(%s.class, %s, \"%s\",\"%s\",\"%s\",%s,\"%s\");\n";
			}else if ("Text".equals(element.getElementExpectName())) {
				code = "solo.clickOnImageView(%s.class, %s, \"%s\",\"%s\",\"%s\", %s,\"%s\");\n";
			}else{
				code = "solo.clickOnImageView(%s.class, %s, \"%s\",\"%s\",%s.class,%s,\"%s\");\n";
			}
			System.out.println(code);
			return String.format(code, element.getElementTypeName(),element.getElementOrder(),element.getPageName(),
					elementOperateDesc==null?"":elementOperateDesc,elementExpectName==null?"":elementExpectName,elementExpectOrder==null?0:elementExpectOrder,elementExpectResult==null?"":elementExpectResult);
		}else {
			if (element.getElementExpectName()==null||element.getElementExpectName().trim().length()==0) {
				code = "solo.clickOnView(%s.class, %s,\"%s\",\"%s\",\"%s\",%s,\"%s\");\n";
			}else if ("Text".equals(element.getElementExpectName())) {
				code = "solo.clickOnView(%s.class, %s,\"%s\",\"%s\",\"%s\",%s,\"%s\");\n";
			}else if ("Id".equals(element.getElementExpectName())) {
				code = "solo.clickOnView(%s.class, %s,\"%s\",\"%s\",\"%s\",%s,\"%s\");\n";
			}else{
				code = "solo.clickOnView(%s.class, %s,\"%s\",\"%s\",%s.class,%s,\"%s\");\n";
			}
			return String.format(code, element.getElementTypeName(),element.getElementOrder(),element.getPageName(),
					elementOperateDesc==null?"":elementOperateDesc,elementExpectName==null?"":elementExpectName,elementExpectOrder==null?0:elementExpectOrder,elementExpectResult==null?"":elementExpectResult);
		}
	}
	private String clickOnWebElement(PageElement element) {
		String code = "";
		switch (element.getElementTypeName()) {
		case SysConstant.ELEMENT_TYPE_TEXT:
			code = "solo.clickOnWebElement(By.textContent(\"%s\"));\n";
			break;
		case SysConstant.ELEMENT_TYPE_TAGNAME:
			code = "solo.clickOnWebElement(By.tagName(\"%s\"));\n";
			break;
		case SysConstant.ELEMENT_TYPE_XPATH:
			code = "solo.clickOnWebElement(By.xpath(\"%s\"));\n";
			break;
		case SysConstant.ELEMENT_TYPE_ID:
			code = "solo.clickOnWebElement(By.id(\"%s\"));\n";
			break;
		case SysConstant.ELEMENT_TYPE_CLASSNAME:
			code = "solo.clickOnWebElement(By.className(\"%s\"));\n";
			break;
		case SysConstant.ELEMENT_TYPE_CSSSELECTOR:
			code = "solo.clickOnWebElement(By.cssSelector(\"%s\"));\n";
			break;
		default:
			break;
		}
		return String.format(code, element.getElementContent());
	}

	private String enterTextInWebElement(PageElement element) {
		String code = "";
		switch (element.getElementTypeName()) {
		case SysConstant.ELEMENT_TYPE_TEXT:
			code = "solo.enterTextInWebElement(By.textContent(\"%s\"), \"%s\");\n";
			break;
		case SysConstant.ELEMENT_TYPE_TAGNAME:
			code = "solo.enterTextInWebElement(By.tagName(\"%s\"), \"%s\");\n";
			break;
		case SysConstant.ELEMENT_TYPE_XPATH:
			code = "solo.enterTextInWebElement(By.xpath(\"%s\"), \"%s\");\n";
			break;
		case SysConstant.ELEMENT_TYPE_ID:
			code = "solo.enterTextInWebElement(By.id(\"%s\"), \"%s\");\n";
			break;
		case SysConstant.ELEMENT_TYPE_CLASSNAME:
			code = "solo.enterTextInWebElement(By.className(\"%s\"), \"%s\");\n";
			break;
		case SysConstant.ELEMENT_TYPE_CSSSELECTOR:
			code = "solo.enterTextInWebElement(By.cssSelector(\"%s\"), \"%s\");\n";
			break;
		default:
			break;
		}
		return String.format(code, element.getElementContent(), element.getElementText());
	}

	private String dragOnView(PageElement element) {
		return null;
	}
	/**
	 * 滑动操作
	 * @param element
	 * @return
	 */
	private String scroll(PageElement element) {
		String code = "";
		switch (element.getElementActionName()) {
		case "drag-down":
			if (element.getDragCount()>1) {
				for (int i = 0; i < element.getDragCount(); i++) {
					code =code + "solo.scrollDown();\n";
				}
			}else {
				code = "solo.scrollDown();\n";
			}
			break;
		case "drag-up":
			if (element.getDragCount()>1) {
				for (int i = 0; i < element.getDragCount(); i++) {
					code =code + "solo.scrollUp();\n";
				}
			}else {
				code = "solo.scrollUp();\n";
			}
			break;
		case "drag-right":
			if (element.getDragCount()>1) {
				for (int i = 0; i < element.getDragCount(); i++) {
					code =code + "solo.scrollToSide(solo.LEFT);\n";
				}
			}else {
				code = "solo.scrollToSide(solo.LEFT);\n";
			}
			break;
		case "drag-left":
			if (element.getDragCount()>1) {
				for (int i = 0; i < element.getDragCount(); i++) {
					code =code + "solo.scrollToSide(solo.RIGHT);\n";
				}
			}else {
				code = "solo.scrollToSide(solo.RIGHT);\n";
			}
			break;
		default:
			break;
		}
		return code;
	}
	/**
	 * input框中输入text信息
	 * @param element
	 * @return
	 */
	private String enterText(PageElement element) {
		String code = "";
		String elementOperateDesc = element.getElementOperateDesc();
		String elementExpectName = element.getElementExpectName();
		Integer elementExpectOrder = element.getElementExpectOrder();
		String elementExpectResult =element.getElementExpectResult();
		String elementText = element.getElementText();
		if ("Y".equals(element.getElementTextFlag())) {
			elementText = elementText+CharacterUtils.getRandomString2(8);
		}
		if (element.getElementContent()!=null&&!"".equals(element.getElementContent())) {
			code = "solo.enterText(\"%s\",%s,\"%s\",\"%s\",\"%s\",\"%s\",%s,\"%s\");\n";
			return String.format(code,element.getElementContent(), element.getElementOrder(),elementText,element.getPageName(),
					elementOperateDesc==null?"":elementOperateDesc,elementExpectName==null?"":elementExpectName,elementExpectOrder==null?0:elementExpectOrder,elementExpectResult==null?"":elementExpectResult);
		}else {
			code = "solo.enterText(%s, \"%s\",\"%s\",\"%s\",\"%s\",%s,\"%s\");\n";
			return String.format(code, element.getElementOrder(),elementText,element.getPageName(),
					elementOperateDesc==null?"":elementOperateDesc,elementExpectName==null?"":elementExpectName,elementExpectOrder==null?0:elementExpectOrder,elementExpectResult==null?"":elementExpectResult);
		}
	}
}
